-
Notifications
You must be signed in to change notification settings - Fork 764
Support cross-locale slug lookup in language switcher #6634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,26 +182,53 @@ def get_visible_document_or_404( | |
except Document.DoesNotExist: | ||
pass | ||
|
||
if ( | ||
not look_for_translation_via_parent | ||
or not (locale := kwargs.get("locale")) | ||
or (locale == settings.WIKI_DEFAULT_LANGUAGE) | ||
): | ||
# We either don't want to try to find the translation via its parent, or it doesn't | ||
# make sense, because we're not making a locale-specific request or the locale we're | ||
# requesting is already the default locale. | ||
if not (locale := kwargs.get("locale")): | ||
raise Http404 | ||
|
||
# We couldn't find a visible translation in the requested non-default locale, so let's | ||
# see if we can find a visible translation via its parent. | ||
slug = kwargs.get("slug") | ||
|
||
if slug and look_for_translation_via_parent: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again many redundant comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing. |
||
# Find documents with this slug in other locales | ||
other_docs_qs = ( | ||
Document.objects.filter(slug=slug).exclude(locale=locale).select_related("parent") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thoughts were that since we are looking for a path to a document through other documents, the documents we look at to move to a valid document should all be collected. We check for visibility later before presenting a final document. What if a document is restricted in one language but not in another? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's problematic and it shouldn't happen. The restricted visibility should be the same across all locales |
||
) | ||
|
||
if locale == settings.WIKI_DEFAULT_LANGUAGE: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels inefficient. We have basically 2 blocks of code (if/else). Each block has the same loop and a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most probably it's cleaner to work with the existence of the parent.doc instead: for doc in other_docs_qs:
base_doc = doc.parent or doc
if base_doc.locale == settings.WIKI_DEFAULT_LANGUAGE:
if base_doc.locale == locale:
return base_doc
translation = base_doc.translated_to(locale, visible_for_user=user)
if translation:
return translation
else:
translation = base_doc.translated_to(locale, visible_for_user=user)
if translation:
return translation |
||
# We're in default locale, look for translations in other locales | ||
for doc in other_docs_qs: | ||
parent_doc = doc.parent or doc | ||
|
||
# If the parent is in the default locale, return it directly | ||
if parent_doc.locale == settings.WIKI_DEFAULT_LANGUAGE: | ||
return parent_doc | ||
|
||
# Otherwise check if it has a translation in the requested locale | ||
translation = parent_doc.translated_to(locale, visible_for_user=user) | ||
if translation: | ||
return translation | ||
else: | ||
# Looking for a non-default locale document | ||
for doc in other_docs_qs: | ||
# Only consider documents that are translations | ||
if not doc.parent: | ||
continue | ||
|
||
# Check if this parent has a translation in our requested locale | ||
translation = doc.parent.translated_to(locale, visible_for_user=user) | ||
if translation: | ||
return translation | ||
|
||
if not look_for_translation_via_parent or locale == settings.WIKI_DEFAULT_LANGUAGE: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this check is so late in the code after all the expensive operations above? This should be combined with the earlier 404. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes - the second check after the loop prevents the final fallback when we're in the default language. Checking it earlier breaks the case where a user requests a document in one locale but is using a slug from another. So - check to see if we should bother finding a translation. |
||
raise Http404 | ||
|
||
# Try to find a translation via the parent | ||
kwargs.update(locale=settings.WIKI_DEFAULT_LANGUAGE) | ||
parent = get_object_or_404(Document.objects.visible(user, **kwargs)) | ||
|
||
# If there's a visible translation of the parent for the requested locale, return it. | ||
if translation := parent.translated_to(locale, visible_for_user=user): | ||
translation = parent.translated_to(locale, visible_for_user=user) | ||
if translation: | ||
return translation | ||
|
||
# Otherwise, we're left with the parent. | ||
if return_parent_if_no_translation: | ||
return parent | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same assertion with the German document?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow you - all these tests have subtle differences, either slug or locale or other flags. Let me know if I've missed something.